package cn.truistic.enmicromsg.wx.wx_friendscircle;

import android.accessibilityservice.AccessibilityService;
import android.text.TextUtils;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.widget.Toast;

import cn.truistic.enmicromsg.app.EnApplication;
import cn.truistic.enmicromsg.common.util.IDConstant;
import cn.truistic.enmicromsg.common.util.PreferenceHelper;
import cn.truistic.enmicromsg.common.util.RootUtil;
import cn.truistic.enmicromsg.common.util.WxHeple;

/**
 * Created by admin on 2018/6/20. 10点16分
 * name: ts
 * 用途： 微信自动评论
 */

public class FriendsCircleHeple extends WxHeple {

    private String friendsContentStr ="  hello!  你好呀！"; //评论的内容
    private boolean isOnePraise = false;
    public FriendsCircleHeple(EnApplication mContext, AccessibilityService mAccessibilityService) {
        super(mContext, mAccessibilityService);
    }

    @Override
    public void isAutomationNearby(AccessibilityEvent event, int eventType) {
        int mNearTextNum = PreferenceHelper.getInt("mFriendTextNum", 0); //获取需要点赞的数量
        if (mEnApplication.isAllowFriendCircle()){
            String friendsContent = PreferenceHelper.getString("FriendsContent" , "");
            if ( !TextUtils.isEmpty(friendsContent) ){
                friendsContentStr = friendsContent;
            }
            copyToBoard(friendsContentStr); //复制到剪切板
            //如果是主界面，并找到发现按钮
            if (isFindText("发现") && isFindId( IDConstant.MAIN_BUTTON )) {
                sleepTime(1000);
                WXfindIdTextAndClick( IDConstant.MAIN_BUTTON, "发现", 2);
                //findTextAndClick("发现", 0);  //点击发现
            }
            if (isFindText("朋友圈")) {
                sleepTime(1000);
                findTextAndClick("朋友圈", 0);  //点击朋友圈
                mEnApplication.setAllowFriendCircle(false);
            }
        }
       else if ( event.getClassName().equals("com.tencent.mm.plugin.sns.ui.SnsTimeLineUI")
                && eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED ) {
            //朋友圈的页面
            for ( int i = 0; i < mNearTextNum ; i++ ) {
                sleepTime(1000 );
                Log.e("TAG", isFindIdlistnum(IDConstant.THUMBS_UP) + ":1");
//                //判定当前有几个评论只有一评论点击第一个，超过两个点击第二个，防止评论被listhead挡住。
                if ( isFindIdlistnum( IDConstant.THUMBS_UP ) > 0 ) {
                    if (!isOnePraise){
                        WXSwitchClick(0);
                        isOnePraise = true;
                    } else {
                        WXSwitchClick(1);
                    }
//                    if ( isFindIdlistnum( IDConstant.THUMBS_UP) >= 4 ) {
//                        WXSwitchClick(1);
//                    } else {
//                        WXSwitchClick(0);
//                    }
                    Log.i("123", "滑动");
                    //翻页操作
                    RootUtil.execSlipCmd( "input swipe 400 1100 400 700" );
                    sleepTime(2000 );
                } else {
                    //翻页操作
                    RootUtil.execSlipCmd( "input swipe 400 1100 400 700" );
                    sleepTime(2000);
                }
            }
            Toast.makeText( mEnApplication , "朋友圈服务结束..", Toast.LENGTH_SHORT ).show();
            mEnApplication.setFriendCircle(false);     //将朋友圈模块开启状态变成false
        }
    }

    //依次点赞
    public void WXSwitchClick(int i ){
        findIdAndClick( IDConstant.THUMBS_UP , i );
        sleepTime(1500 );
        //判断是否已经被点赞
        if ( !isFindText("取消") && isFindId( IDConstant.WX_DIANZAN ) ) {
            //findIdAndClick(IDConstant.WX_DIANZAN, 0);
            //findTextAndClick("赞", 0);
            WXfindIdTextAndClick( IDConstant.WX_DIANZAN  , "赞", 0);
            sleepTime(1500 );
            //点赞操作
            findIdAndClick( IDConstant.THUMBS_UP , i );
        }
        sleepTime(2000);
        if ( !TextUtils.isEmpty(friendsContentStr) && !friendsContentStr.equals("null")) {
            WXfindIdTextAndClick(IDConstant.COMMENT, "评论", 0);
            sleepTime(1500);
            //评论操作
            paste( IDConstant.COMMENT_CONTENT , 0 );
            sleepTime(1500 ) ;
            findIdAndClick( IDConstant.COMMENT_SEND, 0 );
            // findTextAndClick("发送", 0);
            sleepTime(1500 );
        }
    }

}
